package multiNyID.Objek;

/*
 * class MarkerGroup
 *
 * bikin kelas untuk menghubungkan kumpulan multimarker
 * supaya mengacuke satu model
 * --------------------------------------------------------------------------------
 * Based On ColouredTiles.java
 * Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th
 *
 * @author whe_green@yahoo.co.id
 * Analisis Metode Occlusion Based Pada Augmented Reality
 * Studi Kasus Interaksi dengan Objek Virtual Secara Realtime
 * Menggunakan Gerakan Tangan
 *
 * Institut Teknologi Telkom
 * Fakultas Informatika 2007
 * Bandung Indonesia
 *
 * nyargreen.wordpress.com
 */

import com.sun.j3d.utils.image.TextureLoader;
import java.awt.Color;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.ArrayList;


public class ColouredTiles extends Shape3D 
{
  private QuadArray plane;


  public ColouredTiles(ArrayList coords, Color3f col) 
  {
    plane = new QuadArray(coords.size(), 
				GeometryArray.COORDINATES | GeometryArray.COLOR_3 );
    createGeometry(coords, col);
//    createBox(coords);
    createAppearance();
  }
  
 public ColouredTiles(ArrayList coords, String path)
  {
    plane = new QuadArray(coords.size(),
				GeometryArray.COORDINATES | GeometryArray.COLOR_3 );
//    createBox(coords);

    createGeometry(coords, new Color3f());
    createGambar(path);
  }

  private void createGeometry(ArrayList coords, Color3f col)
  { 
    int numPoints = coords.size();

    Point3f[] points = new Point3f[numPoints];
    coords.toArray( points );
    plane.setCoordinates(0, points);

    Color3f cols[] = new Color3f[numPoints];
    for(int i=0; i < numPoints; i++)
      cols[i] = col;
    plane.setColors(0, cols);

    setGeometry(plane);
  }  // end of createGeometry()

  private void createBox(ArrayList coords)
  {
    int numPoints = coords.size();

    Point3f[] points = new Point3f[numPoints];
    coords.toArray( points );
    plane.setCoordinates(0, points);

    Color3f cols[] = new Color3f[numPoints];
    for(int i=0; i < numPoints; i++)
      cols[i] = new Color3f(Color.GREEN);
    plane.setColors(0,cols);

    setGeometry(plane);

  }

  private void createAppearance()
  {
    Appearance app = new Appearance();

    PolygonAttributes pa = new PolygonAttributes();
//    pa.setPolygonMode(pa.POLYGON_LINE);//kalo garis2 aja buat multimarker
    pa.setCullFace(PolygonAttributes.CULL_NONE);
      // so can see the ColouredTiles from both sides

    app.setPolygonAttributes(pa);

    setAppearance(app);
  }  // end of createAppearance()

  private void createGambar(String path)
  {
    Appearance app = new Appearance();
    //Load the image for the texture.
    TextureLoader textureLoad = new TextureLoader(path,null);

    //Generate a (scaled) image of the texture. Height and width must be
    //powers of 2.
    ImageComponent2D textureIm = textureLoad.getScaledImage(16.0f,16.0f);

    //Generate the texture.
    Texture2D aTexture = new Texture2D(Texture2D.BASE_LEVEL,Texture2D.RGB,
                                            textureIm.getWidth(),
                                            textureIm.getHeight());
    aTexture.setImage(0,textureIm);

    //An Appearance which will use the texture.
//    Appearance textureApp = new Appearance();
    app.setTexture(aTexture);
    TextureAttributes textureAttr = new TextureAttributes();
    textureAttr.setTextureMode(TextureAttributes.REPLACE);
    app.setTextureAttributes(textureAttr);
    Material mat = new Material();
    mat.setShininess(100.0f);
    app.setMaterial(mat);
    TexCoordGeneration tcg = new TexCoordGeneration(TexCoordGeneration.OBJECT_LINEAR,
                                                    TexCoordGeneration.TEXTURE_COORDINATE_2);

    app.setTexCoordGeneration(tcg);
    
    PolygonAttributes pa = new PolygonAttributes();
//    pa.setPolygonMode(pa.POLYGON_LINE);//kalo garis2 aja buat multimarker
    pa.setCullFace(PolygonAttributes.CULL_NONE);
      // so can see the ColouredTiles from both sides

    app.setPolygonAttributes(pa);

    setAppearance(app);
  }  // end of createAppearance()
} // end of ColouredTiles class
